package com.oracle.oa.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oracle.oa.dao.BaseDao;
import com.oracle.oa.util.PageBean;
import com.oracle.oa.util.PageBean2;

public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {

	
	public List getAll(Class clzz) {
		List list = null;
		try {
			list = this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(clzz));
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return list;
	}

	
	public Object getById(Class clzz, Serializable id) {
		Object obj = null;
		try {
			obj = this.getHibernateTemplate().get(clzz, id);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}

	
	public List getByProperty(Class clzz, String propertyName,
			Object propertyValue) {
		List list=null;
		try {
			DetachedCriteria dc=DetachedCriteria.forClass(clzz);  //得到指定类型对象
			dc.add(Restrictions.eq(propertyName, propertyValue));  //添加条件
			list=this.getHibernateTemplate().findByCriteria(dc);
			
		/*	String hql="from "+clzz.getSimpleName()+" obj where obj."+propertyName+"=?";
			return this.getHibernateTemplate().find(hql,propertyValue);*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	
	public List getAllByHQL(String hql) {
		List list=null;
		try {
			list=this.getHibernateTemplate().find(hql);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	
	public List getAllByDetachedCriteria(DetachedCriteria dc) {
		List list=null;
		try {
			list=this.getHibernateTemplate().findByCriteria(dc);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return list;
	}

	
	public void save(Object obj) {
		this.getHibernateTemplate().save(obj);
	}

	
	public void update(Object obj) {
		this.getHibernateTemplate().update(obj);
	}

	
	public void delete(Object obj) {
		this.getHibernateTemplate().delete(obj);
	}

	
	public void splitPage1(PageBean pb, DetachedCriteria dc, Class clz) {
		DetachedCriteria dcold = dc;
		String hql = "select count(temp) from " + clz.getSimpleName() + " temp";
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Query query = session.createQuery(hql);

		int count = Integer.parseInt(query.list().get(0).toString());
		pb.setAllRow(count);
		pb.setList(this.getHibernateTemplate().findByCriteria(dc,
				(pb.getCurrentPage() - 1) * pb.getPageSize(), pb.getPageSize()));
		session.close();
	}

	
	public Object getById(Class clzz, Long id) {
		Object obj = null;
		try {
			obj = this.getHibernateTemplate().get(clzz, id);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}

	
	public void fenye(DetachedCriteria dc, PageBean2 pb, Class clz) {
		DetachedCriteria dcold = dc;
		String hql = "select count(temp) from " + clz.getSimpleName() + " temp";
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Query query = session.createQuery(hql);

		int count = Integer.parseInt(query.list().get(0).toString());
		pb.setAllRows(count);
		pb.setList(this.getHibernateTemplate().findByCriteria(dc,
				(pb.getCurrentPage() - 1) * pb.getPageSize(), pb.getPageSize()));
		session.close();
	}
		
	

}
